
var that, go;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    swiperIndex: 0,
    str_time: '',
    id: 0,
    easing: 'easeInOutCubic',
    Insufficient: false,
    hours:'',
    minutes:'',
    seconds:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this
    that.supindex(options.id)
    that.setData({
      id: options.id
    })
    getApp().page.onLoad(this, options)

  },

  onShow: function (e) {
    getApp().page.onShow(this)
  },
  onReady: function (e) {
    setTimeout(function () {
      console.log(that.data.list)
      getApp().request({
        url: getApp().api.surprise.pay_integral,
        data: {
          id: that.data.id,
          goods_id: that.data.list[0].id
        },
        success: function (t) {
          console.log(t)
          if (t.code == 0) {
            if (t.data.pay_integral > 0) {
              getApp().core.showToast({
                title: "您已消耗" + t.data.pay_integral + '颗爱心',
                icon: "none"
              })
            }
          } else if (t.code == 1) {
            wx.showModal({
              title: '提示',
              content: t.msg,
              showCancel: !1,
              confirmText: "确认",
              success(res) {
                if (res.confirm) {
                  console.log('用户点击确定')
                  wx.navigateBack({
                    delta: 1
                  })
                }
              }
            })
          }
        },
        complete: function () {
          getApp().core.hideLoading();
        }
      });
    }, 1000);
  },
  catchTouchMove: function (res) {
    return false
  },
  swiperChange(e) {
    console.log('current',e.detail.current,'list', that.data.list.length,'swiperIndex',that.data.swiperIndex)
    if (e.detail.current == that.data.swiperIndex + 1) {
      var list = that.data.list
      // list.shift()
      console.log('list', list)
      if (e.detail.current+1 == that.data.list.length) {
        that.supindex(that.data.id)
      }
      // setTimeout(
      //   function () {
      that.setData({
        list: list,
        swiperIndex: e.detail.current,
      })
      that.pay_integral(that.data.id)
      // },200)
    }else{
      var arr=that.data.list;
   
        for(var i = 0,len = arr.length;i < len; i++ ){
        var rand = parseInt(Math.random()*len);
        var temp = arr[rand];
        arr[rand] = arr[i];
        arr[i] = temp;
        }
        console.log('arr++',arr);
      console.log('123')
      that.setData({
        list: arr,
        swiperIndex: e.detail.current,
      })
      // that.supindex(that.data.id)
        setTimeout(
        function () {
          that.pay_integral(that.data.id)
        },500)
      
    }

  },
  supindex: function (e) {
    var list = that.data.list
    getApp().request({
      url: getApp().api.surprise.index,
      data: {
        id: e
      },
      success: function (t) {
        console.log(t)
        if (t.code == 0) {
          let start = new Date(t.data.setting.start_time); // 开始时间
          var end_time = t.data.setting.end_time.replace(/-/g, '/')
          let end = new Date(end_time);  // 结束时间
          // that.getLeftTime(start, end);
          that.getDate(end)
          for (var i = 0; i < t.data.goods_list.length; i++) {
            list.push(t.data.goods_list[i])
          }
          that.setData({
            list: list
          })
        } else {
          wx.showModal({
            title: '提示',
            content: t.msg,
            showCancel: !1,
            confirmText: "确认",
            success(res) {
              if (res.confirm) {
                console.log('用户点击确定')
                wx.navigateBack({
                  delta: 1
                })
              }
            }
          })
        }

      },
      complete: function () {
        getApp().core.hideLoading();
      }
    });
  },
  pay_integral: function (e) {
    console.log(that.data.list,that.data.swiperIndex)
    getApp().request({
      url: getApp().api.surprise.pay_integral,
      data: {
        id: e,
        goods_id: that.data.list[that.data.swiperIndex].id
      },
      success: function (t) {
        console.log(t)
        if (t.code == 0) {
          if (t.data.pay_integral > 0) {
            getApp().core.showToast({
              title: "您已消耗" + t.data.pay_integral + '颗爱心',
              icon: "none"
            })
          }
        } else if (t.code == 1) {
          that.setData({
            Insufficient: true
          })
          getApp().core.showModal({
            title: "提示",
            content: t.msg,
            showCancel: !1,
            confirmText: "确认"
          })
        }
      },
      complete: function () {
        getApp().core.hideLoading();
      }
    });
  },
  goodsInfo: function (e) {
    console.log(e)
    var id = e.currentTarget.dataset.goodsId
    wx.navigateTo({
      url: '/pages/surprised-details/surprised-details?goods_id=' + id + '&aid=' + that.data.id,
    })
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearInterval(go);
  },
  getDate(endDateStr) {
    //结束时间
    go = setInterval(function () {
      var endDate = new Date(endDateStr);
      //当前时间
      var nowDate = new Date();
      //相差的总秒数
      var totalSeconds = parseInt((endDate - nowDate) / 1000);
      //天数
      var days = Math.floor(totalSeconds / (60 * 60 * 24));
      //取模（余数）
      var modulo = totalSeconds % (60 * 60 * 24);
      //小时数
      var hours =Math.floor(modulo / (60 * 60)) < 10 ? '0' +Math.floor(modulo / (60 * 60)) :Math.floor(modulo / (60 * 60));
      var hours1 = Math.floor(modulo / (60 * 60));
      modulo = modulo % (60 * 60);
      //分钟
      var minutes = Math.floor(modulo / 60) < 10 ? '0' +Math.floor(modulo / 60) : Math.floor(modulo / 60);
      var minutes1 = Math.floor(modulo / 60);
     
      //秒
      var seconds = modulo % 60 < 10 ? '0' + modulo % 60 : modulo % 60;
      var seconds1 = modulo % 60;
      //输出到页面
      var str_time = '距离活动结束还有' + hours1 + "小时" + minutes1 + "分钟" + seconds1 + "秒"
      // var str_time = '距离活动结束还有'+ days + "天" + hours + "小时" + minutes + "分钟" + seconds + "秒"
      that.setData({
        str_time: str_time,
        hours:hours,
        minutes:minutes,
        seconds:seconds
      })
      if (days == 0 && hours == 0 && minutes == 0 && seconds == 0) {
        wx.showModal({
          title: '提示',
          content: '活动已结束',
          showCancel: !1,
          confirmText: "确认",
          success(res) {
            if (res.confirm) {
              console.log('用户点击确定')
              wx.navigateBack({
                delta: 1
              })
            }
          }
        })
      }
      // console.log("还剩:" + days + "天" + hours + "小时" + minutes + "分钟" + seconds + "秒")
    }, 1000)
  }
})